D:\a\tools.proto\tools.proto\dynamic\src\field\primitive\transform.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::field::primitive::Value; |
30 | | |
31 | | pub trait RawTransform { |
32 | | fn bits_to_raw(&self, bits: u64) -> Value; |
33 | | fn raw_to_bits(&self, value: Value) -> u64; |
34 | | } |
35 | | |
36 | | pub trait ViewTransform { |
37 | | fn raw_to_view(&self, raw: Value) -> Value; |
38 | | fn view_to_raw(&self, view: Value) -> Value; |
39 | | } |
40 | | |
41 | | pub struct SignedTransform { |
42 | | pub max_positive: u64 |
43 | | } |
44 | | |
45 | | impl RawTransform for SignedTransform { |
46 | 12 | fn bits_to_raw(&self, bits: u64) -> Value { |
47 | 12 | if bits > self.max_positive { Branch (47:12): [Folded - Ignored]
Branch (47:12): [True: 7, False: 5]
|
48 | 7 | Value::Signed(-((((!bits) & {self.max_positive}) + 1) as i64)) |
49 | | } else { |
50 | 5 | Value::Signed((bits & self.max_positive) as i64) |
51 | | } |
52 | 12 | } |
53 | | |
54 | 8 | fn raw_to_bits(&self, value: Value) -> u64 { |
55 | | unsafe { |
56 | 8 | std::mem::transmute(value.to_signed()) |
57 | | } |
58 | 8 | } |
59 | | } |
60 | | |
61 | | pub struct BoolTransform; |
62 | | |
63 | | impl RawTransform for BoolTransform { |
64 | 0 | fn bits_to_raw(&self, bits: u64) -> Value { |
65 | 0 | Value::Bool(bits != 0) |
66 | 0 | } |
67 | | |
68 | 0 | fn raw_to_bits(&self, value: Value) -> u64 { |
69 | 0 | if value.to_bool() { Branch (69:12): [Folded - Ignored]
Branch (69:12): [True: 0, False: 0]
|
70 | 0 | 1 |
71 | | } else { |
72 | 0 | 0 |
73 | | } |
74 | 0 | } |
75 | | } |
76 | | |
77 | | pub struct NoneTransform; |
78 | | |
79 | | impl RawTransform for NoneTransform { |
80 | 25 | fn bits_to_raw(&self, bits: u64) -> Value { |
81 | 25 | Value::Unsigned(bits) |
82 | 25 | } |
83 | | |
84 | 7 | fn raw_to_bits(&self, value: Value) -> u64 { |
85 | 7 | value.to_unsigned() |
86 | 7 | } |
87 | | } |
88 | | |
89 | | impl ViewTransform for NoneTransform { |
90 | 30 | fn raw_to_view(&self, raw: Value) -> Value { |
91 | 30 | raw |
92 | 30 | } |
93 | | |
94 | 12 | fn view_to_raw(&self, view: Value) -> Value { |
95 | 12 | view |
96 | 12 | } |
97 | | } |
98 | | |
99 | | pub struct Float32Transform; |
100 | | |
101 | | impl RawTransform for Float32Transform { |
102 | 0 | fn bits_to_raw(&self, bits: u64) -> Value { |
103 | 0 | let value: f32 = unsafe { std::mem::transmute(bits as u32) }; |
104 | 0 | Value::Float(value as _) |
105 | 0 | } |
106 | | |
107 | 0 | fn raw_to_bits(&self, value: Value) -> u64 { |
108 | 0 | let raw: u32 = unsafe { std::mem::transmute(value.to_float() as f32) }; |
109 | 0 | raw as u64 |
110 | 0 | } |
111 | | } |
112 | | |
113 | | pub struct Float64Transform; |
114 | | |
115 | | impl RawTransform for Float64Transform { |
116 | 0 | fn bits_to_raw(&self, bits: u64) -> Value { |
117 | 0 | unsafe { Value::Float(std::mem::transmute(bits)) } |
118 | 0 | } |
119 | | |
120 | 0 | fn raw_to_bits(&self, value: Value) -> u64 { |
121 | 0 | unsafe { std::mem::transmute(value.to_float()) } |
122 | 0 | } |
123 | | } |
124 | | |
125 | | pub struct FloatTransform { |
126 | | pub a: f64, |
127 | | pub b: f64, |
128 | | pub a_inv: f64, |
129 | | pub b_inv: f64, |
130 | | } |
131 | | |
132 | | impl ViewTransform for FloatTransform { |
133 | 7 | fn raw_to_view(&self, raw: Value) -> Value { |
134 | 7 | (raw.to_float() * self.a + self.b).into() |
135 | 7 | } |
136 | | |
137 | 2 | fn view_to_raw(&self, view: Value) -> Value { |
138 | 2 | (view.to_float() * self.a_inv + self.b_inv).into() |
139 | 2 | } |
140 | | } |